exports.__esModule = true;

var _PolygonUtils = require('../../framework/common/utils/PolygonUtils');

var _PolygonUtils2 = _interopRequireDefault(_PolygonUtils);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = class extends think.Service {
  /**
   * 新增单元格
   *
   * @param {any} data
   * @returns
   */
  doAddOrUpdateCell(data) {
    var _this = this;

    return _asyncToGenerator(function* () {
      console.log(111, data);
      const db = _this.db;
      const aj = {
        success: false,
        msg: ''
      };
      const AUTORes = yield _this.CrudService.querySql("SHOW TABLE STATUS WHERE Name='cms_cell'");
      data.scope_path = JSON.stringify(data.scope_path);
      const where = {};
      try {
        if (!data.id) {
          // 新增
          data.BGID = data.area_code + _this.fix(AUTORes[0].Auto_increment, 3);
          data.ORDate = new Date();
          const Res = yield db['cms_cell'].create(data);
          if (Res) {
            aj.success = true;
            aj.result = Res;
            aj.msg = '新增成功';
          } else {
            aj.msg = '插入操作失败';
          }
        } else {
          // 编辑
          const Res = yield db['cms_cell'].update(data, {
            where: {
              id: data.id
            }
          });
          if (Res) {
            aj.success = true;
            aj.msg = '修改成功';
          } else {
            aj.msg = '更新操作失败';
          }
        }

        return aj;
      } catch (error) {
        return _this.exceptionService.handleError(error);
      }
      return aj;
    })();
  }

  /**
   * 获取单元格信息 （分页）
   *
   * @param {any} data
   * @returns
   */
  getCellListPage(data) {
    var _this2 = this;

    return _asyncToGenerator(function* () {
      const db = _this2.db;
      data.start = (parseInt(data.page) - 1) * parseInt(data.pageSize);
      const attributes = ['id', 'BGID', 'area_id', 'BGSQua', 'ORDate', 'CHDate', 'Note', 'scope_path'];
      let where = {};
      if (data.area_id != undefined || data.area_id != null) {
        where = {
          area_id: data.area_id
        };
      }
      try {
        const res = yield db['cms_cell'].findAndCountAll({
          where: where,
          order: [['id', 'DESC']],
          limit: parseInt(data.pageSize),
          offset: parseInt(data.start)
        });
        return res;
      } catch (err) {
        return _this2.exceptionService.handleError(err);
      }
    })();
  }

  /**
   * 通过id查询单元格信息
   *
   * @param {any} data
   * @returns
   */
  getCellListById(data) {
    var _this3 = this;

    return _asyncToGenerator(function* () {
      const db = _this3.db;
      const aj = {
        success: false,
        msg: ''
      };
      const where = {
        area_id: data.area_id
      };
      try {
        const Res = yield db['cms_cell'].findAll({
          where: where
        });
        if (Res) {
          aj.success = true;
          aj.msg = '查询成功';
          aj.result = Res;
        } else {
          aj.msg = '查询错误';
        }
        return aj;
      } catch (err) {
        return _this3.exceptionService.handleError(err);
      }
      return aj;
    })();
  }

  /**
   * 获取所有单元格信息
   *
   * @returns
   */
  getCellAllList() {
    var _this4 = this;

    return _asyncToGenerator(function* () {
      const db = _this4.db;
      try {
        const res = yield db['cms_cell'].findAll();
        return res;
      } catch (err) {
        return _this4.exceptionService.handleError(err);
      }
    })();
  }

  /**
   * 删除单元格信息
   *
   * @param {any} data  data.id
   * @returns
   */
  deltCell(data) {
    var _this5 = this;

    return _asyncToGenerator(function* () {
      const db = _this5.db;
      try {
        const res = yield db['cms_cell'].destroy({
          where: {
            id: data.id
          }
        });
        return res;
      } catch (err) {
        return _this5.exceptionService.handleError(err);
      }
    })();
  }
  /**
   * 通过坐标查询所属社区和所属单元格
   *
   * @param {any} data  data.point
   * @returns
   */
  queryAreaAndBGIDByCoordinate(data) {
    var _this6 = this;

    return _asyncToGenerator(function* () {
      const point = data.point;
      const db = _this6.db;
      try {
        const areaData = yield db['cms_area'].findAll({
          where: {
            scope_path: {
              [_this6.db.Sequelize.Op.not]: null
            }
          }
        });
        const areaRes = areaData.filter(function (item, index) {
          return _PolygonUtils2.default.isPointInPolygon(point, JSON.parse(item.scope_path));
        });
        let cellData = [];
        if (areaRes.length > 0) {
          cellData = yield db['cms_cell'].findAll({
            where: {
              area_id: areaRes[0].id
            }
          });
        }

        const ResData = {
          area: areaRes,
          cell: cellData
        };
        return ResData;
      } catch (err) {
        return _this6.exceptionService.handleError(err);
      }
    })();
  }

  /**
   * 更改数据的位数
   * @param {Number} num  需要更改是数字
   * @param {Number} length       需要更为为几位数据
   */
  fix(num, length) {
    return ('' + num).length < length ? (new Array(length + 1).join('0') + num).slice(-length) : '' + num;
  }
};